 //Useful links:
      // http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker
      // http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
      // http://jqueryui.com/demos/autocomplete/#remote-with-cache
            
      var geocoder;
      var map;
      var marker;

      var map_detail;
      var marker_detail;






          
      function initialize(lati, longi, type){
      //MAP
        var latlng = new google.maps.LatLng(lati, longi);

        var options = {
          zoom: 16,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
              
        map = new google.maps.Map(document.getElementById("map_canvas"), options);

              
        //GEOCODER
        geocoder = new google.maps.Geocoder();
              

    if(type =="detail" ) {

        marker = new google.maps.Marker({
          map: map,
          draggable: false
        });

    }

    if(type =="add" || type == "modif") {
        marker = new google.maps.Marker({
          map: map,
          draggable: true
        });

    }

          if(type =="detail" || type == "modif"){
          marker.setPosition(latlng);
        }
            
                                
      }

  


  function loadmap(type){

                                 
        $(function() {
          $("#address_input").autocomplete({
            //This bit uses the geocoder to fetch address values
            source: function(request, response) {
              geocoder.geocode( {'address': request.term + ',suisse' }, function(results, status) {
                response($.map(results, function(item) {

                  return {
                    label:  item.formatted_address,
                    value: item.formatted_address,
                    latitude: item.geometry.location.lat(),
                    longitude: item.geometry.location.lng()

                  }
                }));
              })
            },
            //This bit is executed upon selection of an address
            select: function(event, ui) {
              if(type =="add") {
              $("#adibilis_adminbundle_activitetype_latitude").val(ui.item.latitude);
              $("#adibilis_adminbundle_activitetype_longitude").val(ui.item.longitude);
            }

            if(type =="modif") {
              $("#adibilis_adminbundle_activiteedittype_latitude").val(ui.item.latitude);
              $("#adibilis_adminbundle_activiteedittype_longitude").val(ui.item.longitude);
            }

              var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
              marker.setPosition(location);
              map.setCenter(location);



            }
          });
        });
        
        //Add listener to marker for reverse geocoding
        google.maps.event.addListener(marker, 'drag', function() {
          geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                $('#address_input').val(results[0].formatted_address);
                if(type =="add") {
                $('#adibilis_adminbundle_activitetype_latitude').val(marker.getPosition().lat());
                $('#adibilis_adminbundle_activitetype_longitude').val(marker.getPosition().lng());
              }

               if(type =="modif") {
                $('#adibilis_adminbundle_activiteedittype_latitude').val(marker.getPosition().lat());
                $('#adibilis_adminbundle_activiteedittype_longitude').val(marker.getPosition().lng());
            }

              }
            }
          });
        });



}


      $(document).ready(function() { 
          $('#myCarousel').oneCarousel({
                    easeIn: 'rotateIn',
                    interval: 15000,
                    pause: 'hover'
                });





                var $menu_select = $("<select />");
                $("<option />", { "selected": "selected", "value": "", "text": "Site Navigation" }).appendTo($menu_select);
                $menu_select.appendTo(".header .navbar-inner");

                $(".header .navbar-inner ul li a").each(function () {
                    var menu_url = $(this).attr("href");
                    var menu_text = $(this).text();
                    if ($(this).parents("li").length == 2) { menu_text = '- ' + menu_text; }
                    if ($(this).parents("li").length == 3) { menu_text = "-- " + menu_text; }
                    if ($(this).parents("li").length > 3) { menu_text = "--- " + menu_text; }
                    $("<option />", { "value": menu_url, "text": menu_text }).appendTo($menu_select)
                });

                field_id = ".header .navbar-inner select";
                $(field_id).change(function () {
                    value = $(this).attr('value');
                    window.location = value;
                    //go
                });





                initialize(latitud, longitud, type);

                loadmap(type);

                 






                 
                  
                                 















        
      });